﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using GGEditor.API;
using GGEditor.API.Plugins;
using GGEditor.API.Samples.PluginTreeViewer;
using GGEditor.Core;
using GGEditor.Core.Documents;
using GGEditor.Core.Editors;
using GGEditor.Core.Plugins;
using GGEditor.Core.UI;
using GGEditor.API.Utility.Forms;

namespace GGEditor.Windows
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			GlobalProperties.Instance.Set("Environment.Platform", "Windows");
			GlobalProperties.Instance.Set("Environment.Paths.ConfigurationDirs",
				new string[]
				{
					Path.GetFullPath(Path.Combine(Path.Combine(
						Path.GetDirectoryName(Application.ExecutablePath),
						"..\\..\\..\\.."), "config")),
					Path.Combine(Environment.GetFolderPath(
						Environment.SpecialFolder.ApplicationData),
						"GGEditor")
				});

			XmlNodeObjectFactory.Instance.RegisterFromAssembly(
				Assembly.GetAssembly(typeof(XmlNodeObjectFactory)));

			DefaultPluginLoader.Instance.LoadDefaultPlugins();
			
			// Initialize the plugin context for API plugins
			PluginContext.Initialize(PluginManager.Instance);

			// Setup the main window
			IWorkspaceForm mainForm =
				PluginContext.Instance.GetObject<IWorkspaceForm>
					("/Workspace/Window");
			mainForm.Show();

			DocumentManager.Instance.New("LandAndWaterMap");
			DocumentManager.Instance.New("TerrainViewer");
            DocumentManager.Instance.New("TileEditor");

			//string filePath = @"C:\map.law";

			//if (File.Exists(filePath))
			//    DocumentManager.Instance.Open(filePath);
			//else
			//    DocumentManager.Instance.New("LandAndWaterMap");

			//Application.ApplicationExit += delegate
			//{
			//    DocumentManager.Instance.ActiveDocument.SaveAs(filePath);
			//};

			Application.Run(mainForm.Form);
		}
	}
}
